package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.CartConverter;
import com.zqj.project.dto.CartDTO;
import com.zqj.project.service.CartService;
import com.zqj.project.vo.CartVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description: CartController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/cart")
@Tag(name = "Cart API", description = "购物车管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class CartController {

    // 自动生成依赖注入的构造函数
    private final CartService cartService;

    // 分页查询购物车列表
    @Operation(summary = "分页查询购物车列表", description = "根据分页参数查询购物车信息")
    @PostMapping("/page")
    public R<IPage<CartVO>> queryCartPage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<CartDTO> pageParams) {
        CartDTO cartDTO = pageParams.getModel();
        if (Objects.isNull(cartDTO.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        return R.success(CartConverter.toCartVoPage(this.cartService.page(CartConverter.toCartModel(cartDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize()))));
    }

    // 将商品加入购物车
    @Operation(summary = "将商品加入购物车", description = "新增一条新的购物车记录")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public R<String> add(@RequestBody @Validated @Parameter(description = "要新增的购物车信息") CartDTO cartDto) {
        return Boolean.TRUE.equals(this.cartService.add(CartConverter.toCartModel(cartDto))) ? R.success("加入购物车成功") : R.fail("加入购物车失败");
    }

    // 修改购物车
    @Operation(summary = "修改购物车", description = "根据购物车ID更新购物车信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要修改的购物车信息，需包含ID") CartDTO cartDto) {
        if (Objects.isNull(cartDto.getId())) {
            return R.fail("购物车ID不能为空");
        }
        return Boolean.TRUE.equals(this.cartService.edit(CartConverter.toCartModel(cartDto))) ? R.success("购物车修改成功") : R.fail("购物车修改失败");
    }

    // 清空购物车
    @Operation(summary = "清空购物车", description = "根据购物车ID删除购物车信息")
    @DeleteMapping("/delete/{userId}")
    @PreventDuplicateSubmit
    public R<String> delete(@PathVariable @Parameter(description = "要清空的购物车的用户ID", required = true) Long userId) {
        return this.cartService.delete(userId) > 0 ? R.success("清空购物车成功") : R.fail("清空购物车失败");
    }

}